const { ccclass, property } = cc._decorator;
import MonsterControl from './MonsterControl';
import WorldNodeControl from '../World/WorldNodeControl';
import MonstersBilderControl from './MonstersBilderControl';
import SkillBuild from '../SkillBuild/SkillBuild';
import Skill from '../SkillBuild/Skill';

enum createType {
    random = 0,//随机位置生成
    active = 1,//场景中任意位置生成
    heroin = 2//Hero附近生成
}

@ccclass
export default class MonstersBilder extends cc.Component {
    @property(cc.Node)
    ObjectNode: cc.Node = null;
    @property(cc.Node)
    WorldNode: cc.Node = null;
    @property(cc.Node)
    MonsterNode: cc.Node = null;
    @property(cc.Node)
    Monster: cc.Node = null;
    @property([cc.String])
    skills: string[] = [];
    @property
    SkillBuildControl: SkillBuild = null;
    poolNode: cc.NodePool = new cc.NodePool();
    MonsterControl: number = 0;
    WorldControl: WorldNodeControl = null;
    BilderControl: MonstersBilderControl = null;

    onLoad() {
        this.WorldControl = this.WorldNode.getComponent(WorldNodeControl);
    }
    getMonster(): cc.Node {
        let monster: cc.Node = null;
        if (this.poolNode.size() > 0) {
            monster = this.poolNode.get();
        }
        else {
            monster = cc.instantiate(this.Monster);
            let control = monster.getComponent(MonsterControl);
            control.monstersBilder = this;
            let skillNode = monster.getChildByName("Skills");
            this.skills.forEach(element => {
                let skill = this.SkillBuildControl.GetSkill(element);
                let skillControl = skill.getComponent(Skill);
                skillControl.SourceNode = monster.getChildByName("launch");
                skillControl.damageAttack = control.attackDamage;
                skillControl.init();
                skillNode.addChild(skill);
            });
        }

        this.MonsterControl++;
        return monster;
    }
    getCheckWay(CreateType: createType): cc.Node {
        let way: cc.Node = null;
        for (let i = 0; i < 3; i++) {
            way = this.getWay(CreateType);
            if (way)
                return way;
            else if (CreateType != createType.random)
                CreateType = CreateType - 1;
        }
    }
    getWay(CreateType: createType): cc.Node {
        if (CreateType == createType.heroin) {
            let hh = HeroHelper.getInstance();
            let flyId = hh.uuidWayCollider;
            let child: cc.Node = null;
            child = this.ObjectNode.getChildByUuid(flyId);
            if (child) {
                return child;
            }
        }
        if (CreateType == createType.active) {
            let activeFlyWays = this.WorldControl.GetWaysByActive(true, null);
            if (activeFlyWays && activeFlyWays.length >= 1) {
                let nandu = Math.floor(Math.random() * (activeFlyWays.length));
                let flyId = activeFlyWays[nandu];
                let child: cc.Node = null;
                child = this.ObjectNode.getChildByUuid(flyId.id);
                if (child) {
                    return child;
                }
            }
        }
        else {
            if (this.WorldControl) {
                let nandu = Math.floor(Math.random() * 3);
                if (nandu == 0 && this.WorldControl.flyWays) {
                    let index = Math.floor(Math.random() * this.WorldControl.flyWays.length);
                    return this.WorldControl.flyWays[index];
                }
                else if (nandu == 1 && this.WorldControl.runWays) {
                    let index = Math.floor(Math.random() * this.WorldControl.runWays.length);
                    return this.WorldControl.runWays[index];
                }
                else {
                    let index = Math.floor(Math.random() * this.WorldControl.staticWays.length);
                    return this.WorldControl.staticWays[index];
                }
            }
        }
        return null;
    }
    recycleMonster(Monster: cc.Node) {
        this.MonsterControl--;
        this.poolNode.put(Monster);
    }
}